package com.demo.wcx.greendaodemo;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by Administrator on 2016-05-26.
 */
public abstract class MyAdapter extends BaseAdapter {
    private List<UserBean> list;
    private Context context;

    public MyAdapter(Context context, List<UserBean> list) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item, null);

            holder.name = (TextView) convertView.findViewById(R.id.name);
            holder.id = (TextView) convertView.findViewById(R.id.id);
            holder.phone = (TextView) convertView.findViewById(R.id.phone);
            holder.del = (ImageView) convertView.findViewById(R.id.del);
            holder.edit = (ImageView) convertView.findViewById(R.id.edit);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.id.setText(list.get(position).getId()+"");
        holder.name.setText(list.get(position).getName());
        holder.phone.setText(list.get(position).getPhone());
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                del(position, v);
            }
        });
        holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit(position, v);
            }
        });

        return convertView;
    }

    public abstract void del(int position, View view);

    public abstract void edit(int position, View view);

    public final class ViewHolder {
        public TextView name;
        public TextView id;
        public TextView phone;
        public ImageView del;
        public ImageView edit;
    }
}
